package org.apache.calcite.sql.ddl;

import org.apache.calcite.schema.ColumnStrategy;
import org.apache.calcite.sql.*;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.pretty.SqlPrettyWriter;
import org.apache.calcite.sql.util.SqlString;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.function.UnaryOperator;

public class CustomSqlColumnDeclaration extends SqlColumnDeclaration{
    private String tag;
    CustomSqlColumnDeclaration(SqlParserPos pos, SqlIdentifier name, SqlDataTypeSpec dataType, @Nullable SqlNode expression, ColumnStrategy strategy) {
        super(pos, name, dataType, expression, strategy);
    }

    public CustomSqlColumnDeclaration(SqlParserPos pos, SqlIdentifier name, SqlDataTypeSpec dataType, @Nullable SqlNode expression, ColumnStrategy strategy, String tag) {
        super(pos, name, dataType, expression, strategy);
        this.tag = tag;
    }


    @Override
    public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
        name.unparse(writer, 0, 0);
        dataType.unparse(writer, 0, 0);
        if (Boolean.FALSE.equals(dataType.getNullable())) {
            writer.keyword("NOT NULL");
        }
        if("primaryKey".equals(tag)){
//            writer.keyword("primary key");
            ((MySqlPrettyWriter)writer).primary();
        }
        SqlNode expression = this.expression;
        if (expression != null) {
            switch (strategy) {
                case VIRTUAL:
                case STORED:
                    writer.keyword("AS");
                    exp(writer, expression);
                    writer.keyword(strategy.name());
                    break;
                case DEFAULT:
                    writer.keyword("DEFAULT");
                    exp(writer, expression);
                    break;
                default:
                    throw new AssertionError("unexpected: " + strategy);
            }
        }
    }
}
